﻿using IcsSEMCtrl.Db;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Unit;
using Lifter.Gui.Monitor.Panels;
using Lifter.Gui.Widget;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels
{
    /// <summary>
    /// LiHaoYu
    /// </summary>
    class LoadMainLayout
    {
        //定义Table参数
        public int floorCount;
        public int rows;
        public int columns;
        //定义Port,CV List
        public List<BMDTLifterPortWidget> controlPortList = new List<BMDTLifterPortWidget>();
        public List<CnvBuffer> controlCVList = new List<CnvBuffer>();
        public List<VehicleWidget> controlVehicleList = new List<VehicleWidget>();
        public delegate void delegateUpdateCVControl(string controlID, string CVType);
        public delegateUpdateCVControl DoDelegateUpdateCVControl;
        //单例模式
        private static LoadMainLayout loadMainLayout = null;
        
        public static LoadMainLayout getLoadMainLayout()
        {
            if (loadMainLayout == null)
            {
                loadMainLayout = new LoadMainLayout();
            }
            return loadMainLayout;
        }

        public enum ControlType
        {
            PORT,
            CV,
            VEHICLE,
        }

        public LoadMainLayout()
        {
            //getTableConfigSQL();
            getUnitControlSQL();
            this.DoDelegateUpdateCVControl += new delegateUpdateCVControl(UpdateCVControl);
        }
        /// <summary>
        /// 按CV方向更新控件是否需要创建 
        /// </summary>
        public void UpdateCVControl(string controlID, string CVType)
        {
            if (!string.IsNullOrEmpty(controlID))
            {
                foreach (CnvBuffer cnvBuffer in controlCVList)
                {
                    if (controlID.Equals(cnvBuffer.controlName))
                    {
                        if (ConveyorBuffer.Constants.CVUNLOAD.Equals(CVType))
                        {
                            if (!cnvBuffer.ContextMenuStrip.Items.Contains(cnvBuffer.CreateCommand))
                            {
                                cnvBuffer.ContextMenuStrip.Items.Add(cnvBuffer.CreateCommand);
                            }
                        }
                        if (ConveyorBuffer.Constants.CVLOAD.Equals(CVType))
                        {
                            if (cnvBuffer.ContextMenuStrip.Items.Contains(cnvBuffer.CreateCommand))
                            {
                                cnvBuffer.ContextMenuStrip.Items.Remove(cnvBuffer.CreateCommand);
                            }
                        }
                    }
                }
            }
        }
        public void getUnitControlSQL()
        {
            DBConnection connection = DBControl.GetConnection();
            string sqlString = "SELECT * FROM MainUILayout";
            SqlDataReader sqlDataReader = connection.ExecuteQuery(sqlString);
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetLifter();
            while (sqlDataReader.Read())
            {
                try
                {
                    //查询数据赋值给控件类字段
                    string type = sqlDataReader["Type"].ToString();
                    if (ControlType.PORT.ToString().Equals(type))
                    {
                        BMDTLifterPortWidget bMDTLifterPortWidget = new BMDTLifterPortWidget();
                        bMDTLifterPortWidget.controlName = sqlDataReader["ControlName"].ToString();
                        bMDTLifterPortWidget.controlHostID = lifterSEM.HostID + sqlDataReader["ControlHostID"].ToString();
                        bMDTLifterPortWidget.axis_X = int.Parse(sqlDataReader["Axis_X"].ToString());
                        bMDTLifterPortWidget.axis_Y = int.Parse(sqlDataReader["Axis_Y"].ToString());
                        bMDTLifterPortWidget.width = int.Parse(sqlDataReader["Width"].ToString());
                        bMDTLifterPortWidget.height = int.Parse(sqlDataReader["Height"].ToString());
                        bMDTLifterPortWidget.ElementId = sqlDataReader["ControlName"].ToString();

                        //加入PortList
                        controlPortList.Add(bMDTLifterPortWidget);
                    }
                    else if (ControlType.CV.ToString().Equals(type))
                    {
                        CnvBuffer cnvBuffer = new CnvBuffer();
                        cnvBuffer.controlName = sqlDataReader["ControlName"].ToString();
                        cnvBuffer.controlHostID = lifterSEM.HostID + sqlDataReader["ControlHostID"].ToString();
                        cnvBuffer.axis_X = int.Parse(sqlDataReader["Axis_X"].ToString());
                        cnvBuffer.axis_Y = int.Parse(sqlDataReader["Axis_Y"].ToString());
                        cnvBuffer.width = int.Parse(sqlDataReader["Width"].ToString());
                        cnvBuffer.height = int.Parse(sqlDataReader["Height"].ToString());
                        cnvBuffer.ElementId = sqlDataReader["ControlName"].ToString();

                        LifterSEM lftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
                        ConveyorBuffer conveyorBuffer = lftSem.GetConveyorBuffer(cnvBuffer.controlHostID);

                        if (conveyorBuffer != null)
                        {
                            if (ConveyorBuffer.Constants.CVUNLOAD.Equals(conveyorBuffer.Cvtype))
                            {
                                cnvBuffer.ContextMenuStrip.Items.Add(cnvBuffer.CreateCommand);
                            }
                        }

                        //加入CVList
                        controlCVList.Add(cnvBuffer);
                    }
                    else if (ControlType.VEHICLE.ToString().Equals(type))
                    {
                        VehicleWidget vehicleWidget = new VehicleWidget();
                        vehicleWidget.controlName = sqlDataReader["ControlName"].ToString();
                        vehicleWidget.controlHostID = lifterSEM.HostID + sqlDataReader["ControlHostID"].ToString();
                        vehicleWidget.axis_X = int.Parse(sqlDataReader["Axis_X"].ToString());
                        vehicleWidget.axis_Y = int.Parse(sqlDataReader["Axis_Y"].ToString());
                        vehicleWidget.width = int.Parse(sqlDataReader["Width"].ToString());
                        vehicleWidget.height = int.Parse(sqlDataReader["Height"].ToString());
                        vehicleWidget.ElementId = sqlDataReader["ControlName"].ToString();
                        //加入CVList
                        controlVehicleList.Add(vehicleWidget);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(connection);
        }
    } 
}
